#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "queue.h"

//创建
QueueArray* createQueueArray(size_t len)
{
    QueueArray *queue = malloc(sizeof(QueueArray));
    queue->base = malloc(sizeof(TYPE) * (len + 1));
    queue->len = len + 1;
    queue->rear = 0;
    queue->front = 0;
    return queue;
}
//销毁
void destoryQueueArray(QueueArray* queue)
{
    free(queue->base);
    free(queue);
}
//队满
bool fullQueueArray(QueueArray* queue)
{
    return (queue->rear + 1) % queue->len == queue->front;
}
//队空
bool emptyQueueArray(QueueArray* queue)
{
    return queue->rear == queue->front;
}
//入队
bool pushQueueArray(QueueArray* queue,TYPE val)
{
    if(fullQueueArray(queue))
        return false;
    queue->base[queue->rear] = val;
    queue->rear = (queue->rear + 1) % queue->len;
    return true;
}
//出队
bool popQueueArray(QueueArray* queue)
{
    if(emptyQueueArray(queue))
        return false;
    queue->front = (queue->front + 1) % queue->len;
    return true;
}
//查看队头
TYPE frontQueueArray(QueueArray* queue)
{
    return queue->base[queue->front];
}

TYPE rearQueueArray(QueueArray* queue)
{
    return queue->base[(queue->rear - 1 + queue->len) % queue->len];
}

//队中元素数量
size_t sizeQueueArray(QueueArray *queue)
{
    return (queue->front - queue->rear + queue->len) % queue->len;
}
